package com.bdf.ipharma.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.springframework.context.ApplicationContext;

import com.bdf.ipharma.ApplicationContextProvider;
import com.bdf.ipharma.config.Session;
import com.bdf.ipharma.model.map.DispenserTipoMap;
import com.bdf.ipharma.model.map.ProductoStockMap;
import com.bdf.ipharma.service.ProductoService;
import com.bdf.ipharma.starter.McdError;
import com.bdf.ipharma.starter.McdMain;
import com.bdf.ipharma.starter.McdPrs;
import javax.swing.JScrollPane;
import javax.swing.JButton;

public class GUIChoiceAction extends GUIWindow {

	private GUIButtonImage jBtnExtraccion = null;
	private GUIButtonImage jBtnReposicion = null;
	
	public Session session;
	
	public GUIChoiceAction(McdMain mcdMain2) throws Exception {
		super(mcdMain2);
		this.session = mcdMain2.getSession();
		this.init();
	}

	private void init()  throws Exception {
		
		this.setOpaque(false);

		RoundedPanel roundedPanel = new RoundedPanel();
		roundedPanel.setBackground(new Color(255, 255, 255));
		roundedPanel.setOpaque(false);
		jBtnExtraccion = new GUIButtonImage(Images.buttOkLong, Images.buttOkDnLong);
		
		jBtnExtraccion.setText("Extraccion de Producto");

		jBtnReposicion = new GUIButtonImage(Images.buttOkLong, Images.buttOkDnLong);
		
		jBtnReposicion.setText("Reposicion de Producto");

		GroupLayout gl_roundedPanel = new GroupLayout(roundedPanel);
		gl_roundedPanel.setHorizontalGroup(gl_roundedPanel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_roundedPanel
						.createSequentialGroup()
						.addGap(27)
						.addGroup(
								gl_roundedPanel
										.createParallelGroup(Alignment.LEADING, false)
										.addComponent(jBtnReposicion, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(jBtnExtraccion, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 464,
												GroupLayout.PREFERRED_SIZE)).addContainerGap(27, Short.MAX_VALUE)));
		gl_roundedPanel.setVerticalGroup(gl_roundedPanel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_roundedPanel.createSequentialGroup().addGap(60)
						.addComponent(jBtnExtraccion, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE).addGap(18)
						.addComponent(jBtnReposicion, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(60, Short.MAX_VALUE)));
		roundedPanel.setLayout(gl_roundedPanel);
		this.setSize(800, 650);
		
		//JButton btnCancelar = new JButton("CANCELAR");
		GUIButtonImage btnCancelar = new GUIButtonImage("Cancelar", Images.buttGenericGrey, Images.buttGenericGreyDn);
		
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//CANCELAR
				System.out.println("cancelarrr");
				session.disposeSession();
				mcdMain.start();

			}
		});
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(141)
					.addComponent(roundedPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(141, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(368, Short.MAX_VALUE)
					.addComponent(btnCancelar)
					.addGap(343))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(230)
					.addComponent(roundedPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(67)
					.addComponent(btnCancelar)
					.addContainerGap(140, Short.MAX_VALUE))
		);
		setLayout(groupLayout);

		/** Aca comienzan los action performed**/
		
		jBtnExtraccion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				DispenserTipoMap parametrosDispenser = (DispenserTipoMap) session.get(Texts.DISPENSER_PROPERTIES);
				ApplicationContext applicationContext = ApplicationContextProvider.getInstance().getApplicationContext();
				ProductoService productoService = (ProductoService) applicationContext.getBean("productoService");
				List<ProductoStockMap> listProductos = productoService.selectAllProductos(String.valueOf(parametrosDispenser.getIdDispenser()));
				
				/**Controla que los productos a mostrar esten dentro de las X,Y filas, columnas dentro del dispenser seleccionado **/			     		        
		        /* TODO: ver como solucionamos esto FUNCIONALMENTE
				Iterator iterator = listProductos.iterator();
		        
		        for (int i = 0; i < listProductos.size(); i++) {
		        	ProductoStockMap prodExt = (ProductoStockMap)iterator.next();
		        	if(prodExt.getFila() > parametrosDispenser.getMaxFilas() || prodExt.getColumna() > prodExt.getColumna()){
		        		listProductos.remove(i);
		        	}
		        	
				}
				*/
				boolean sinProductos = true;
				for(ProductoStockMap controlStockCero: listProductos){
					if(controlStockCero.getCantidad()>0)
						sinProductos = false;
				}
				
				try {
					session.isTimeout(new Date());
					session.setTimeout(new Date());
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(
							JOptionPane.getFrameForComponent((Component)e.getSource()),
							Texts.MENSAJE_SESSION_TIMEOUT);
					
					session.disposeSession();
					mcdMain.start();
					return;
				}
				
				if(sinProductos){
					JOptionPane.showMessageDialog(
							JOptionPane.getFrameForComponent((Component)e.getSource()),
							McdPrs.MENSAJE_DISPENSER_SIN_PRODUCTOS_PARA_EXTRAER);
					
					session.disposeSession();
					mcdMain.start();
					return;
				}
				
				
				menu(e.getActionCommand());
			}
		});
		
		jBtnReposicion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menu(e.getActionCommand());
			}
		});
	}

	
	
   public void menu(String action) {
	   GuiExtraction2 guiExtraccionProd;
	   GUIReposicionProd guiReposicionProd;
		
	      try {
	    	  if(action.equalsIgnoreCase("Extraccion de Producto")){
	    		  guiExtraccionProd = new GuiExtraction2(this.mcdMain);
		          this.mcdMain.setWindow(guiExtraccionProd);  
	    	  }
	    	  else{
	    		  guiReposicionProd= new GUIReposicionProd(this.mcdMain);
		          this.mcdMain.setWindow(guiReposicionProd);
	    	  }
	    		  
	          
	      } catch (Exception e) {
	         McdError.add(e);
	      }
	   }
	
   
   public void paintComponent(Graphics g) {
		g.drawImage(Images.bgChoiceAction.getImage(), 0, 0, null);

	}
   
	private static final long serialVersionUID = 1L;
}